mod serializer;
mod preprocessor;
mod types;
mod error;
mod byte_cursor;
pub use serializer::Serializer;

#[cfg(test)]
mod tests {
    use std::io::Write;

    use serde::Serialize;

    use super::*;

    #[derive(Serialize)]
    struct A{
        i1:i32,
        i2:i64,
        f1:f32,
        f2:f64,
        test:String
    }

    #[derive(Serialize)]
    struct B{
        i1:i32,
        i2:i64,
        f1:f32,
        f2:f64,
        a1:A,
        a2:A,
        e1:E
    }

    #[derive(Serialize)]
    enum E {
        ED{str:String}
    }

    #[test]
    fn test_counter(){
        
    
        let test_b = B{
            i1: 1,
            i2: 2,
            f1: 1.0,
            f2: 1.1,
            a1: A{
                i1: 1,
                i2: 1,
                f1: 1.0,
                f2: 1.1,
                test: "test".to_string()
            },
            a2: A{
                i1: 1,
                i2: 1,
                f1: 1.0,
                f2: 1.1,
                test: "haha".to_string()
            },
            e1:E::ED { str: "gan111".to_owned() }
        };
        let processer = preprocessor::Preprocessor::new();
        let result = test_b.serialize(processer);
        if let Ok(data) = result {
            println!("{:?} {:?}", data.borrow(), data.borrow().gengrate_refdata().get_ref_array().len());
        }
        else {
            println!("error");
        }

        let result = crate::serializer::Serializer::serialize(&test_b);
        if let Ok(vecu8) = result {
            println!("{:?}", vecu8.len());
            write_bytes_to_file("test.bin", &vecu8).unwrap();
        }
        else {
            println!("error");
        }
        // let data = processer.data.borrow().clone();
        // println!("{:?}", );
    
    }

    fn write_bytes_to_file(filename: &str, data: &[u8]) -> std::io::Result<()> {
        let mut file = std::fs::File::create(filename)?;
        file.write_all(data)?;
        Ok(())
    }
}
 